package fr.webeo.teambuilder.ao;

import java.util.Collections;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import fr.webeo.teambuilder.bo.Player;

/**
 * The class <strong>MyTableModel</strong>.
 * 
 * @author Sébastien Bordes
 * @version $Revision$ $Date$ $Name$
 * 
 * @since fr.webeo.teambuilder 1.0
 */
public class MyTableModel extends AbstractTableModel {

    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = -1122190760078428801L;
    public static final int COL_READY = 0;
    public static final int COL_NOM = 1;
    public static final int COL_PRENOM = 2;
    public static final int COL_MAIL = 3;
    public static final int COL_EQUIPE = 4;
    public static final int COL_COEQUIPIER = 5;
    public static final String NAME_COL_READY = "Ready";
    public static final String NAME_COL_NOM = "Nom";
    public static final String NAME_COL_PRENOM = "Pr\351nom";
    public static final String NAME_COL_MAIL = "eMail";
    public static final String NAME_COL_EQUIPE = "Pr\351f\351rence";
    public static final String NAME_COL_COEQUIPIER = "Coequipier";
    public static final String NAME_COL_OPPONENT = "Adversaire";

    /** . */
    private final List<Player> content;

    /**
     * 
     * @param v
     */
    public MyTableModel(List<Player> v) {
        content = v;
        Collections.sort(content);
        addEmptyLine();
    }

    public int getColumnCount() {
        return 6;
    }

    public Player getValueAt(int rowIndex) {
        return content.get(rowIndex);
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {

        case 0:
            return new Boolean(content.get(rowIndex).isReady());

        case 1:
            return content.get(rowIndex).getLastName();

        case 2:
            return content.get(rowIndex).getFirstName();

        case 3:
            return content.get(rowIndex).getMail();

        case 4:
            return content.get(rowIndex).getTeamPref();

        case 5:
            return content.get(rowIndex).getTeamMatePref();

        case 6:
            return content.get(rowIndex).getOpponentPref();
        }
        return null;
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        switch (column) {
        case 0:
            content.get(row).setReady(((Boolean) aValue).booleanValue());
            break;

        case 1:
            content.get(row).setLastName((String) aValue);
            break;

        case 2:
            content.get(row).setFirstName((String) aValue);
            break;

        case 3:
            content.get(row).setMail((String) aValue);
            break;

        case 4:
            content.get(row).setTeamPref((String) aValue);
            break;

        case 5:
            content.get(row).setTeamMatePref((String) aValue);
            break;
        case 6:
            content.get(row).setOpponentPref((String) aValue);
            break;
        }
        fireTableCellUpdated(row, column);
    }

    /**
	 * 
	 */
    @Override
    public boolean isCellEditable(int row, int column) {
        return column != 0 || getValueAt(row).isValid();
    }

    /**
	 * 
	 */
    @Override
    public Class getColumnClass(int columnIndex) {
        return getValueAt(columnIndex).getClass();
    }

    /**
	 * 
	 */
    @Override
    public String getColumnName(int column) {
        switch (column) {
        case 0:
            return NAME_COL_READY;

        case 1:
            return NAME_COL_NOM;

        case 2:
            return NAME_COL_PRENOM;

        case 3:
            return NAME_COL_MAIL;

        case 4:
            return NAME_COL_EQUIPE;

        case 5:
            return NAME_COL_COEQUIPIER;

        case 6:
            return NAME_COL_OPPONENT;
        }
        return null;
    }

    /**
	 * 
	 */
    public int getRowCount() {
        return content.size();
    }

    /**
	 * 
	 */
    public void addEmptyLine() {
        content.add(new Player());
    }

    /**
	 * 
	 */
    public void delete(int selectedRow) {
        content.remove(selectedRow);
        fireTableRowsDeleted(selectedRow, selectedRow);
    }
}
